using System;
using System.Collections.Generic;
using UnityEngine;

namespace ZhanQi.EventGraph.Models
{
    // 事件触发类型枚举
    [Serializable]
    public enum EventTriggerType
    {
        [InspectorName("生命周期_开始模式")] Start = 0,
        [InspectorName("生命周期_结束模式")] Finish = 1,
        [InspectorName("导航_跳转模式")] Jump = 2,
        [InspectorName("输入输出_输入模式")] Enter = 3,
        [InspectorName("输入输出_输出模式")] Output = 4,
        [InspectorName("交互_选择卡牌模式")] PlaceCards = 5,
        [InspectorName("交互_按钮模式")] ButtonControl = 6,
        [InspectorName("交互_判断模式")] JudgmentEvent = 7,
        [InspectorName("交互_执行模式")] Implement = 8,
        [InspectorName("展示_插图模式")] Illustration = 9,
        [InspectorName("展示_文本模式")] Text = 10,
        [InspectorName("展示_对话模式")] Dialogue = 11
    }

    // 卡牌类型枚举
    [Serializable]
    public enum CardType
    {
        Role,
        Good,
        Skill,
        Real,
        Event,
        Item,
        Support
    }

    // 事件连线信息类
    [Serializable]
    public class EventLink
    {
        public string key;
        public string value;
        public string color;
        public string expression;
        public bool isEnter;

        public EventLink(string key = "", string value = "", string color = "")
        {
            this.key = key;
            this.value = value;
            this.color = color;
        }
    }

    // 事件连线字典类
    [Serializable]
    public class EventLinkDict
    {
        public List<EventLink> links = new List<EventLink>();

        public string this[string key]
        {
            get
            {
                var link = links.Find(e => e.key == key);
                return link != null ? link.value : null;
            }
            set
            {
                var link = links.Find(e => e.key == key);
                if (link != null) link.value = value;
                else links.Add(new EventLink { key = key, value = value });
            }
        }
    }

    // 卡槽信息类
    [Serializable]
    public class CardSlot
    {
        public List<Card> Cards = new List<Card>();
    }

    // 卡片信息类
    [Serializable]
    public class Card
    {
        public string Name;
        public CardType cardType;
    }

    // 数据对象类
    [Serializable]
    public class DataObj
    {
        public string name;
        public string value;
        public Type type;
    }

    // 数据对象字典类
    [Serializable]
    public class DataObjDict
    {
        public List<DataObj> DataObj = new List<DataObj>();

        public string this[string key]
        {
            get
            {
                var link = DataObj.Find(e => e.name == key);
                return link != null ? link.value : null;
            }
            set
            {
                var link = DataObj.Find(e => e.name == key);
                if (link != null) link.value = value;
                else DataObj.Add(new DataObj { name = key, value = value });
            }
        }
    }

    // 数据类型枚举
    [Serializable]
    public enum Type
    {
        Int,
        Float,
        String,
        Bool,
    }

    /// <summary>
    /// 表示一个事件节点的数据模型。
    /// 使用 UnityEngine.JsonUtility 进行序列化/反序列化。
    /// </summary>
    [Serializable]
    public class NodeData
    {
        public override string ToString()
        {
            return JsonUtility.ToJson(this, true);
        }
        public string nodeID;               // 唯一标识 ID
        public string eventName;            // 事件名称
        public CardSlot cardSlots;          // 卡槽信息
        public EventTriggerType triggerType;// 事件触发类型
        public string triggerIntroduce;     // 触发事件说明文字
        public EventLinkDict EventId;       // 连线目标字典
        public Vector2 position;            // 节点在画布上的位置
        public Vector2 size;                // 节点尺寸
        public string IconImagePath;        // 图标图片路径
        public ImageSliceInfo IconSliceInfo;// 图标切片信息
        public EventTriggeringPhase TriggeringPhase; // 事件触发时机
        public string JumpEventName;        // 跳转事件名称
        public EndMode endMode;             // 结束模式
        public string EnterObjName;         // 输入对象名称
        public Type EnterObjType;           // 输入对象类型
        public string OutputObjName;        // 输出对象名称
        public Type OutputObjType;          // 输出对象类型
        public DataObjDict DataObjDict;     // 附加数据字段字典
        public List<DialogueItem> dialogueItems = new List<DialogueItem>(); // 事件对话数据
    }

    // 对话角色位置枚举
    [Serializable]
    public enum DialogueRolePosition
    {
        [InspectorName("左")] Left,
        [InspectorName("中")] Middle,
        [InspectorName("右")] Right,
    }

    // 对话数据类
    [Serializable]
    public class DialogueItem
    {
        public string Name;
        public string IconImagePathLeft;
        public ImageSliceInfo IconSliceInfoLeft;
        public string IconImagePathMiddle;
        public ImageSliceInfo IconSliceInfoMiddle;
        public string IconImagePathRight;
        public ImageSliceInfo IconSliceInfoRight;
        public DialogueRolePosition MainInterviewer;
        public string Value;
        public override string ToString()
        {
            return JsonUtility.ToJson(this, true);
        }
    }

    // 事件图数据类
    [Serializable]
    public class EventGraphData
    {
        public override string ToString()
        {
            return JsonUtility.ToJson(this, true);
        }
        public List<NodeData> nodes = new List<NodeData>();
    }

    // 事件触发阶段枚举
    [Serializable]
    public enum EventTriggeringPhase
    {
        [InspectorName("开始游戏触发")] StartTheGameTrigger,
        [InspectorName("进入游戏触发")] EnterTheGameToTrigger,
        [InspectorName("物品进入背包触发")] ItemsEnterTheBackpackAndTriggerIt,
        [InspectorName("每回合触发")] TriggeredPerTurn,
        [InspectorName("战斗触发")] FightTrigger,
        [InspectorName("每天触发")] TriggeredEveryDay,
        [InspectorName("每小时触发")] TriggeredEveryHour,
        [InspectorName("立即触发")] Immediately,
    }

    // 结束模式枚举
    [Serializable]
    public enum EndMode
    {
        [InspectorName("递归")] Recursion,
        [InspectorName("切换及结束")] SwitchAndEnd,
    }
}